//用状态机实现：字符串“abcabx”的解析

function match(str){
    let state = start;
    for (const c of str) {
        state = state(c);

    }
    return state == end;
}

function end(c){
    return end;
}

function start(c){
    if(c=="a"){
        return foundB;
    }else{
        return start;
    }
}

function foundB(c){
    if(c=="b"){
        return foundC;
    }else{
        return start(c);
    }
}

function foundC(c){
    if(c=="c"){
        return foundA;
    }else{
        return start(c);
    }
}

function foundA(c){
    if(c=="a"){
        return foundB2;
    }else{
        return start(c);
    }
}

function foundB2(c){
    if(c=="b"){
        return foundX;
    }else{
        return start(c);
    }
}

function foundX(c){
    if(c=="x"){
        return end;
    }else{
        return start(c);
    }
}

console.log(match("afaabcabxfadfa"))
